#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#include "../../../../mac/mac.h"

#if 0
typedef struct mac_scan_req_t_ {
    mac_scan_type_t   scan_type;
    uint8             scan_duration;
    mac_scan_chnls_t  scan_chnls;
    /* as security is not supported, the security related members are omitted */
} mac_scan_req_t;

typedef struct mac_scan_chnls_t_ {
    uint8 *chnls;
    uint8 num;
} mac_scan_chnls_t;

#endif

int8 mac_init_test(uint16 pan_id)
{
    mac_retry_init();
    mac_indir_init();
    buf_init();
    mac_info_init();
    mac_set_pan_id(pan_id);
    rf_init();
}


int8 mac_ed_scan_test()
{
    uint8 r;
    mac_scan_req_t ed;
    mac_scan_chnls_t *s = &ed.scan_chnls;
    mac_scan_cfm_t *cfm;
    uint8 c = 11;
    int8 peak;

    ed.scan_duration = 2;
    s->chnls = &c;
    s->num = 1;

    MAC_DEBUG("mac_ed_scan_test: 1\n");
    r = mac_ed_scan(&ed);
    if (r != MAC_OK) {
        return MAC_ERR;
    }
    
    MAC_DEBUG("mac_ed_scan_test: 2\n");
    cfm = mac_nhl_get_scan_cfm();
    peak = rf_get_sml_ed_val();

    if (cfm->status != MAC_OK) {
        return MAC_ERR;
    }

    MAC_DEBUG("mac_ed_scan_test: 3\n");
    MAC_DEBUG("mac_ed_scan_test: peak value is %d\n", peak);
    MAC_DEBUG("mac_ed_scan_test: cmf->energy_detect_list = %d\n", *(cfm->energy_detect_list));

    if (*(cfm->energy_detect_list) != peak) {
        return MAC_ERR;
    }
    MAC_DEBUG("mac_ed_scan_test: peak value is %d\n", peak);
    
    return MAC_OK;
}

#if 0
typedef struct mac_scan_req_t_ {
    mac_scan_type_t   scan_type;
    uint8             scan_duration;
    mac_scan_chnls_t  scan_chnls;
    /* as security is not supported, the security related members are omitted */
} mac_scan_req_t;

typedef struct mac_scan_chnls_t_ {
    uint8 *chnls;
    uint8 num;
} mac_scan_chnls_t;
#endif

int8 mac_active_scan_test(uint num)
{
    uint32 t, now;
    mac_scan_req_t req;
    uint8 chnls_list[NUM_OF_CHNLS_SUP] = {11, 12, 13, 14, 15, 16, 17, 18,
                                          19, 20, 21, 22, 23, 24, 25, 26,};
    mac_scan_chnls_t *scan_chnls = &req.scan_chnls;
    scan_chnls->num = num;
    scan_chnls->chnls = chnls_list;
    req.scan_duration = 3;
    
    t = timer_get();
    mac_active_scan(&req);

    /* delay for a while to give time to the timeout timer */
    while (now = timer_get()) {
        if ((int32)(now-t) > 200000) {
            break;
        }
    }

    return MAC_OK;
}

int8 get_bcn_frm_test()
{
    uint8 *tx_frm;
    buf_t *recv;
    mac_hdr_t hdr;
    uint8 l;

    recv = buf_alloc(true);
    if (recv == NULL) {
        return MAC_ERR;
    }

    tx_frm = get_tx_frm();
    l = get_tx_frm_len();
    /* back off the pointer */
    MAC_DEBUG("get_bcn_frm_test: length of the generated frame is %d\n", l);
    recv->ptr -= (l-1);
    memcpy(recv->ptr, tx_frm, l);

    mac_parse_hdr(recv, &hdr);
    MAC_DEBUG("get_bcn_frm_test: hdr.ctrl.frm_type = %d\n", hdr.ctrl.frm_type);

    if (hdr.ctrl.frm_type != FRM_TYPE_CMD) {
        return MAC_ERR;
    }

    return MAC_OK;
}

uint16 get_pan_id_test()
{
    return mac_get_pan_id();
}

